import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from common.base import Base
from common.base import open_browser

# 商品浏览目标网页
shoppingcart_url = 'http://139.129.26.163/'


class BrowseGoodsPage(Base):
    shop_cart_loc = (By.XPATH, '//*[@id="ECS_CARTINFO"]/a')  # 购物车定位
    refresh_shop_cart_loc = (By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[2]')  # 更新购物车定位
    continue_shop_loc = (By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a')  # 继续购物定位
    camera_loc = (By.XPATH, '/html/body/div[6]/div/div/div[2]/div[2]/div/a[1]/div[1]/img')  # 智能相机定位
    buy_now_loc = (By.XPATH, '//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a')  # 立即购买定位
    delete_loc = (By.XPATH, '/html/body/div[6]/div[1]/form/table[1]/tbody/tr[2]/td[7]')  # 删除
    empty_shop_cart_loc = (By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[1]')  # 清空购物车定位

    # 点击进入购物车
    def click_shopping_cart(self):
        self.click(self.shop_cart_loc)

    # 点击继续购物
    def click_continue_shop(self):
        self.click(self.continue_shop_loc)

    # 点击智能相机,进入商品详情页面
    def click_camera(self):
        time.sleep(3)
        self.click(self.camera_loc)

    # 点击立即购买,添加至购物车
    def click_buy_now(self):
        self.click(self.buy_now_loc)

    # 点击更新购物车
    def click_refresh_shopping_cart(self):
        self.click(self.refresh_shop_cart_loc)

    # 点击删除商品并确定
    def click_delete(self):
        self.click(self.delete_loc)
        self.alert_operation()

    # 点击清空购物车
    def click06_empty_shopping_cart(self):
        self.click(self.empty_shop_cart_loc)


if __name__ == '__main__':
    shop = BrowseGoodsPage(open_browser())
    shop.get(shoppingcart_url)
    shop.window()
    shop.click_shopping_cart()  # 进入购物车
    time.sleep(2)
    shop.click_continue_shop()  # 点击继续购物
    time.sleep(2)
    shop.click_camera()  # 点击智能相机
    time.sleep(2)
    shop.click_buy_now()  # 点击立即购买
    time.sleep(2)
    shop.click_delete()  # 点击删除
    time.sleep(3)
    shop.click_refresh_shopping_cart()  # 点击更新购物车
    time.sleep(2)
    shop.click_continue_shop()  # 点击继续购物
    time.sleep(2)
    shop.click_camera()  # 点击智能相机
    time.sleep(2)
    shop.click_buy_now()  # 点击立即购买
    time.sleep(2)
    shop.click06_empty_shopping_cart()  # 清空购物车
    time.sleep(1)
    shop.quit()
